/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package model;

import java.sql.Date;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import databean.FundPriceHistoryBean;



public class FundPriceHistoryDAO {
	private BeanFactory<FundPriceHistoryBean> factory;
	
	public FundPriceHistoryDAO() throws DAOException {
		try {
			// Get a BeanTable so we can create the "user" table
	        BeanTable<FundPriceHistoryBean> fundPriceHistoryTable = BeanTable.getInstance(FundPriceHistoryBean.class,"fundpricehistory");
	        
	        if (!fundPriceHistoryTable.exists()) fundPriceHistoryTable.create("fundID", "priceDate");
	        
	        // Long running web apps need to clean up idle database connections.
	        // So we can tell each BeanTable to clean them up.  (You would only notice
	        // a problem after leaving your web app running for several hours.)
	        fundPriceHistoryTable.setIdleConnectionCleanup(true);
	
	        // Get a BeanFactory which the actions will use to read and write rows of the "user" table
	        factory = fundPriceHistoryTable.getFactory();
	        
	        
	        
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		}
	}
	
	public void create(FundPriceHistoryBean bean) throws DAOException {
		try {
	    	
        	Transaction.begin();
        	
        	FundPriceHistoryBean dbBean = factory.create(bean.getFundID(), bean.getPriceDate());
        	dbBean.setPrice(bean.getPrice());
			
			Transaction.commit();
			
		} catch (DuplicateKeyException e) {
			throw new DAOException(e);
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	/**
	 * lookup FundPriceHistoryBean by fund id and price date
	 * @param name
	 * @return
	 * @throws DAOException
	 */
	public FundPriceHistoryBean lookup(long fundID, Date priceDate) throws DAOException {
		try {
			return factory.lookup(fundID, priceDate);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	/**
	 * lookup history by fundID
	 * @param fundID
	 * @return
	 * @throws DAOException
	 */
	public List<FundPriceHistoryBean> lookup(long fundID) throws DAOException {
		FundPriceHistoryBean[] total =getFundPriceHistory();
		ArrayList<FundPriceHistoryBean> rs=new ArrayList<FundPriceHistoryBean>();
		for(FundPriceHistoryBean his:total){
			if(his.getFundID()==fundID){
				rs.add(his);
			}
		}
		return rs;
	}
	
	
	/**
	 * get formatted price
	 * @param fundID
	 * @param priceDate
	 * @return
	 * @throws DAOException
	 */
	public String getPriceFormatted(long fundID, Date priceDate) throws DAOException {
		FundPriceHistoryBean bean = lookup(fundID, priceDate);
		long price = bean.getPrice();
		
		NumberFormat formatter = new DecimalFormat("##0.00");
		String s = formatter.format(((double)price/100));
		
		return s;
	}
		
	protected BeanFactory<FundPriceHistoryBean> getFactory() { return factory; }
	
	public FundPriceHistoryBean[] getFundPriceHistory() throws DAOException {
		try {
			FundPriceHistoryBean[] fundPriceHistory = factory.match();
			Arrays.sort(fundPriceHistory);
			return fundPriceHistory;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
}
